<template>
	<view>
		<uni-popup ref="consultEndPopup" height="200px" type="center" :animation="false">
			<view class="mypopup">
				<view class="mypopup__head">
					<image src="../../static/img/icon-clock.png"></image>
					<text>咨询完工</text>
				</view>
				<view class="mypopup__body">
					<view v-if="IsShowMaintainInfo">
						<checkbox-group @change="ChangeChecked" class="block" data-name="delayRemainder">
							<view class="cu-form-group">
								<text class="text-sm">是否需要延迟提醒</text>
								<checkbox class='round blue' :checked="IsDelayRemainder" />
							</view>
						</checkbox-group>
						<checkbox-group @change="ChangeChecked" class="block" data-name="delayToNextTime" v-if="IsDelayRemainder">
							<view class="cu-form-group">
								<text class="text-sm">延迟到下次保养时间</text>
								<checkbox class='round blue' :checked="DelayToNextTime" />
							</view>
						</checkbox-group>
						<view v-if="!DelayToNextTime && IsDelayRemainder">
							<picker mode="selector" :range="unitList" :value="Index" range-key="Name" @change="ChangeunitList">
								<AGInput customStyle="margin-top:0px;" readonly dropdown placeholder="延迟提醒时间单位" :value="unitList[Index].Name" />
							</picker>
							<AGInput placeholder="延迟提醒时间" :value="DelayDate" @change="ChangeDelayDate" type="number" />
						</view>
					</view>
					<picker mode="selector" :range="consultEndReason" :value="reasonIndex" range-key="Name" @change="ChangeReason">
						<AGInput customStyle="margin-top:0px;" readonly dropdown placeholder="原因代码" :value="consultEndReason[reasonIndex].Name" />
					</picker>
					<view class="ag-divider"></view>
					<AGInput textarea placeholder="备注" @change="ChangeConsultEndRemark" />
					<view class="mypopup__footer">
						<button class="cu-btn lg primary" @click="onConsultEndSubmit">确定</button>
						<button class="cu-btn lg primary plain" @click="Cancel">取消</button>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from '@dcloudio/uni-ui/lib/uni-popup/uni-popup';
	import AGInput from '../AGInput/index'
	import constant from '@/common/constant'
	export default {
		props: {
			IsShowMaintainInfo: {
				type: Boolean,
				default () {
					return false
				}
			},
			WoId:{
				type: String,
				default () {
					return ""
				}
			},
		},
		components: {
			AGInput,
			uniPopup
		},
		data() {
			return {
				consultEndReason: [],
				reasonIndex: 0,
				ConsultEndRemark: "",
				//是否需要延迟提醒
				IsDelayRemainder: true,
				//延迟到下次保养时间
				DelayToNextTime: true,
				//延迟提醒时间单位
				DelayDateUnit: "",
				//延迟提醒时间
				DelayDate: "",
				//单位列表
				unitList: [{
					Name: "",
					Value: "",
				}, {
					Name: "天",
					Value: "1",
				}, {
					Name: "周",
					Value: "2",
				}, {
					Name: "月",
					Value: "3",
				}],
				Index: 0,
			}
		},
		mounted() {
			this.ReasonCodeListAsync(constant.WORDER_consult_code)
		},
		methods: {
			onConsultEndSubmit(e) {
				let ret = this.CheckData();
				if(!ret)
					return;
				let parm = {
					Remark: this.ConsultEndRemark,
					ReasonID: this.consultEndReason[this.reasonIndex].ReasonCodeId,
					IsDelayRemainder: this.IsDelayRemainder,
					DelayToNextTime: this.DelayToNextTime,
					DelayDateUnit: this.unitList[this.Index].Value,
					DelayDate: this.DelayDate,
				}
				this.$emit('onConsultEndSubmit', parm);
			},
			Cancel(e) {
				this.closePoPup();
			},
			ChangeConsultEndRemark(e) {
				console.log(e);
				this.ConsultEndRemark = e;
			},
			ChangeReason(e) {
				//console.log(e);
				this.reasonIndex = e.detail.value;
			},
			openPoPup() {
				this.$refs.consultEndPopup.open();
			},
			closePoPup() {
				this.$refs.consultEndPopup.close();
			},

			ReasonCodeListAsync(ReasonCodeType) {
				uni.showLoading({
					mask: true,
				})
				let UserPhone = this.UserInfo.mobile;
				let token = this.UserInfo.RetId
				this.workorderdetailservice.ReasonCodeListAsync(UserPhone, ReasonCodeType, token,this.WoId)
					.then(result => {
						console.log(result);
						if (result.RetBool == true) {
							let data = JSON.parse(result.RetObject);
							this.consultEndReason = data;
						} else {
						   uni.showToast({
						   	 title: result.RetMsg,
						   	 icon: "none"
						   });
						}
						uni.hideLoading();
					}).catch(c=>{
						 uni.hideLoading();
					});
			},
			
			ChangeChecked(e) {
				let checkedName = e.currentTarget.dataset.name
				if (checkedName == 'delayRemainder') {
					this.IsDelayRemainder = !this.IsDelayRemainder;
				} else if (checkedName == 'delayToNextTime') {
					this.DelayToNextTime = !this.DelayToNextTime;					
				}
			},
			//时间数变更
			ChangeDelayDate(e) {
				this.DelayDate = e
			},
			//时间单位
			ChangeunitList(e) {
				this.Index = e.detail.value;
			},
			//检验数据
			CheckData() {
				if (this.IsDelayRemainder == true) {
					if (this.DelayToNextTime == false) {
						let valDelayDateUnit = this.unitList[this.Index].Value;
						if (valDelayDateUnit == "") {
							uni.showToast({
								title: "请选择延迟提醒时间单位！",
								icon: "none"
							});
							return false;
						} else if (this.DelayDate == "") {
							// 返回必填错误信息提示
							uni.showToast({
								title: "请填写延迟提醒时间！",
								icon: "none"
							});
							return false;
						}
					}else{
						this.Index = 0;
						this.DelayDate = ""
					}
				}
				return true;
			},
		}
	}
</script>

<style lang="scss">
</style>
